//
//  YhxUtility.m
//  UEPersonage
//
//  Created by mac on 2017/9/15.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "YhxUtility.h"
#import <AVFoundation/AVFoundation.h>

@implementation YhxUtility

+ (float)autoCalculateWidthOrHeight:(float)height
                             width:(float)width
                          fontsize:(float)fontsize
                           content:(NSString*)content
{
    //计算出rect
    CGRect rect = [content boundingRectWithSize:CGSizeMake(width, height)
                                        options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                     attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]} context:nil];
    
    //判断计算的是宽还是高
    if (height == MAXFLOAT) {
        return rect.size.height;
    }
    else
        return rect.size.width;
}


+ (float)autoSizeToThatWidth:(float)width fontsize:(float)fontsize content:(NSString *)content
{

    UILabel *docName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,width, 0)];
    docName.numberOfLines = 0;
    docName.font = [UIFont systemFontOfSize:fontsize];
    docName.text = content;
    CGSize size = [docName sizeThatFits:CGSizeMake(docName.frame.size.width, MAXFLOAT)];
    return size.height;
}

+ (void)setExtraCellLineHidden: (UITableView *)tableView
{
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    [shapeLayer setStrokeColor:lineColor.CGColor];
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    [lineView.layer addSublayer:shapeLayer];
}

+ (void)checkUSerIfAllowNotification{
    
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0f) {
        
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        
        if (UIUserNotificationTypeNone == setting.types) {
            
            shareData.userIfOpenPush = NO;
        }
        else
        {
            shareData.userIfOpenPush = YES;
        }
    }
    else
    {
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        
        if(UIRemoteNotificationTypeNone == type){
            shareData.userIfOpenPush = NO;
        }
        else
        {
            shareData.userIfOpenPush = YES;
        }
    }
}


+ (UIImage *)createImageWithColor:(UIColor*) color{
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

+ (UIView *)cirleImageWithView:(UIView *)view
{
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:view.frame.size.width] addClip];
    [view drawRect:view.bounds];
    //结束画图
    UIGraphicsEndImageContext();
    return view;
}


+ (NSString *)replaceTypePath:(NSString *)str{
    
    TGLog(@"type == %@",[UserDefaults objectForKey:@"jeemaaUserTypeDefPath"]);
    NSString *string = [str stringByReplacingOccurrencesOfString:@"personal" withString:[UserDefaults objectForKey:@"jeemaaUserTypeDefPath"]];
    TGLog(@"返回url = %@",string);
    return string;
}

@end
